<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="系统模块" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入系统模块"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="operName">
        <el-input
          v-model="queryParams.operName"
          placeholder="请输入操作人员"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="businessType">
        <el-select
          v-if="dict.sys_oper_type"
          v-model="queryParams.businessType"
          placeholder="操作类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dd in dict.sys_oper_type.dictData"
            :key="dd.dictCode"
            :label="dd.dictLabel"
            :value="dd.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-if="dict.sys_common_status"
          v-model="queryParams.status"
          placeholder="操作状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dd in dict.sys_common_status.dictData"
            :key="dd.dictCode"
            :label="dd.dictLabel"
            :value="dd.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
        >清空</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <!--操作日志列表-->
    <el-table ref="tables" :data="list" :default-sort="defaultSort" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日志编号" align="center" prop="operId" />
      <el-table-column label="系统模块" align="center" prop="title" />
      <el-table-column label="操作类型" align="center" prop="businessType">
        <template slot-scope="scope">
          <span v-if="scope.row.businessType==1">新增</span>
          <span v-if="scope.row.businessType==2">修改</span>
          <span v-if="scope.row.businessType==3">删除</span>
          <span v-if="scope.row.businessType==4">授权</span>
          <span v-if="scope.row.businessType==5">导出</span>
          <span v-if="scope.row.businessType==6">导入</span>
          <span v-if="scope.row.businessType==7">强退</span>
          <span v-if="scope.row.businessType==8">生成代码</span>
          <span v-if="scope.row.businessType==9">清空数据</span>
        </template>
      </el-table-column>
      <el-table-column label="请求方式" align="center" prop="requestMethod" />
      <el-table-column label="操作人员" align="center" prop="operName" width="100" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']" />
      <el-table-column label="操作地址" align="center" prop="operIp" width="130" :show-overflow-tooltip="true" />
      <el-table-column label="操作地点" align="center" prop="operLocation" :show-overflow-tooltip="true" />
      <el-table-column label="操作状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status==0">成功</span>
          <span v-if="scope.row.status==1">失败</span>
        </template>
      </el-table-column>
      <el-table-column label="操作日期" align="center" prop="operTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.operTime) }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="queryParams.pageNum"-->
    <!--      :limit.sync="queryParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->

    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：" prop="title">{{ form.title }} / </el-form-item>
            <el-form-item
              label="登录信息："
            >{{ form.operName }} / {{ form.operIp }} / {{ form.operLocation }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址：">{{ form.operUrl }}</el-form-item>
            <el-form-item label="请求方式：">{{ form.requestMethod }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ form.method }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ form.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">{{ form.jsonResult }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作状态：">
              <div v-if="form.status === 0">正常</div>
              <div v-else-if="form.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间：">{{ form.operTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="form.status === 1" label="异常信息：">{{ form.errorMsg }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!--分页-->
    <el-pagination
      :current-page="current"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>

import qs from 'qs'
const axios = require('axios')

export default {
  name: 'Operlog',
  data() {
    return {
      dicts: ['sys_oper_type', 'sys_common_status'],
      dict: {},
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: {},
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {},
      current: 1,
      size: 10
    }
  },
  created() {
    this.getList(this.current, this.size)
    this.getDictData()
  },
  methods: {
    /** 查询字典 */
    getDictData() {
      this.axios.get('http://localhost:8080/dict/maps/' + this.dicts.join())
        .then(res => {
          this.dict = res.data
        })
    },
    /** 查询登录日志 */
    getList(current, size) {
      axios.post('http://localhost:8080/intelligence_community/sys-oper-log/operLogList?current=' + current + '&size=' + size + '&dateRange=' + this.dateRange.join(), this.queryParams)
        .then(res => {
          this.list = res.data.records
          this.current = res.data.current
          this.total = res.data.total
        })
    },

    /** 操作日志类型字典翻译/
      typeFormat(row, column) {

      },

      /** 搜索按钮操作 */
    handleQuery(current, size) {
      current = 1
      size = this.size
      this.getList(current, size)
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.current = 1
      this.dateRange = []
      this.queryParams = {}
      this.$refs.tables.clearSort()
      this.handleQuery()
    },

    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.operId)
      this.multiple = !selection.length
    },

    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop
      this.queryParams.isAsc = column.order
      this.getList(this.current, this.size)
    },

    /** 详细按钮操作 */
    handleView(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const operIds = row.operId || this.ids
      this.$messagebox.confirm('是否确认删除日志编号为"' + operIds + '"的数据项？').then(() => {
        axios.delete('http://localhost:8080/intelligence_community/sys-oper-log/operLogDelete/' + operIds)
          .then(res => {
            if (res.data == '删除成功') {
              this.$message.warning('删除成功')
              this.getList(this.current, this.size)
            } else {
              this.$message.warning('删除失败')
            }
          })
      }).catch(() => {})
    },

    /** 清空按钮操作 */
    handleClean() {
      this.$messagebox.confirm('是否确认清空所有登录日志数据项？').then(function() {
        axios.get('http://localhost:8080/intelligence_community/sys-oper-log/emptyDatabase')
      }).then(() => {
        location.reload()
        this.$message.warning('清空成功')
      }).catch(() => {})
    },

    /** 导出按钮操作 */
    handleExport() {
      axios({
        method: 'get',
        url: 'http://localhost:8080/intelligence_community/sys-oper-log/downloadExcel',
        responseType: 'arraybuffer'
      })
        .then(res => {
          console.log(res)
          // 使用Blob对象将后端返回的Arraybuffer格式得数据转成blob类型的数据 window.URL.createObjectURL(blob)可以把一个blob转成blobURL，用作图片显示，文件下载（不能再服务器上存储，只能在浏览器使用）
          const b = new Blob([res.data], { type: 'application/vnd.ms-excel' })
          // 使用URL对象将blob类型的数据生成一个在线下载的链接
          const url = URL.createObjectURL(b)
          // 然后我们使用a标签进行下载
          const link = document.createElement('a')

          // 设置导出的文件名
          link.download = '操作日志'
          link.href = url
          // 点击获取文件
          link.click();

          (err) => {
            console.log(err)
          }
        })
    },

    //  分页
    handleSizeChange(val) {
      this.size = val
      this.getList(this.current, val)
    },
    handleCurrentChange(val) {
      this.getList(val, this.size)
    }
  }
}
</script>

<style scoped>

</style>
